package com.resou.reader.dialog.threeDaysrecharge;

import android.util.ArrayMap;

import com.resou.reader.ReSouApplication;
import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.entry.WXPayBean;
import com.resou.reader.api.service.ActivityService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.mine.paycenter.AliPay;
import com.resou.reader.utils.DeviceUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ReaderRechargePresenter extends ResouBasePresenter<ReaderRechargeContract.View> implements ReaderRechargeContract.Presenter {
    private ActivityService mService;

    public ReaderRechargePresenter(ReaderRechargeContract.View view) {
        super(view);
        mService = ApiImp.getInstance().getService(ActivityService.class);
    }

    /**
     * 获取支付宝订单信息
     */
    public void fetchAliPayOrderInfo(String price) {
        mView.showLoadingIndicator();
        String title = "热币充值";

        String token = UserInstance.getToken();
        mService.aliPayActivity(token, price)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AliPay>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(AliPay aliPay) {
                        mView.hideLoadingIndicator();
                        mView.invokeAliPay(aliPay.getUrl());
//                        ToastUtil.makeLongToast(aliPay.getUrl());
//    //统计用户充值哪个金额比较多
////                        HashMap<String, String> map = new HashMap<>();
////                        map.put("type", title);
////                        map.put("amount", price);
////                        MobclickAgent.onEvent(mView.getViewContext(), "pay_amount", map);

////                        map.clear();
////                        map.put("充值说明", title);
////                        map.put("充值金额", price);
////                        MobclickAgent.onEvent(mView.getViewContext(), "pay_amount1", map);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取微信订单信息
     */
    public void fetchWXOrderInfo(String price) {
        mView.showLoadingIndicator();
        String title = "热币充值";

        mService.wxPayAndroidActivity(UserInstance.getToken(), price, title,
                DeviceUtils.getIMEI(ReSouApplication.getRSApplication()))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<WXPayBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<WXPayBean> result) {
                        if (result == null) {
                            return;
                        }
                        mView.hideLoadingIndicator();
                        mView.invokeWXPay(result.getData());
                        //统计用户充值哪个金额比较多
                        Map<String, String> map = new ArrayMap<>();
                        map.put("type", title);
                        map.put("amount", price);
                        MobclickAgent.onEvent(ReSouApplication.getRSApplication(), "pay_amount", map);

                        map.clear();
                        map.put("充值说明", title);
                        map.put("充值金额", price);
                        MobclickAgent.onEvent(ReSouApplication.getRSApplication(), "pay_amount1", map);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
